package com.landtool.lanbase.shipping;

import cn.hutool.core.date.DateUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.landtool.lanbase.constant.CacheConsts;
import com.landtool.lanbase.entity.ShipEntity;
import com.landtool.lanbase.netty.GlobalUtil;
import com.landtool.lanbase.utils.FileUtils;
import com.landtool.lanbase.utils.ZipUtil;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Component
public class PushService {

    private static Logger log = LoggerFactory.getLogger(PushService.class);

    @Autowired
    private RedisTemplate <String,Object> redisTemplate;

    /**
     * 推送数据分批数量
     */
    private static Integer SizeNum = 1500;

    /**
     * 推送船只数据     * @param map 处理后的船只数据
     */
    public void pushShipData(HashMap <String, List <ShipEntity>> map) {
        List <ShipEntity> update = map.get("Update");
        List <ShipEntity> delete = map.get("Delete");
        List <ShipEntity> add = map.get("Add");
        judgeType(update, "Update");
        judgeType(delete, "Delete");
        judgeType(add, "Add");
    }

    /**
     * 判断推送的类型
     * @param date 船只数据
     * @param type 推送的类型：Update，Delete，Add
     */
    private void judgeType(List <ShipEntity> date, String type) {
        List <List <ShipEntity>> results = FileUtils.splitList(date, SizeNum);
        Gson gson = new GsonBuilder().serializeNulls().create();
        for (int i = 0; i < results.size(); i++) {
            List <ShipEntity> shipEntities = results.get(i);
            String info ="";
            if("Update".equals(type)){
                log.info("推送了"+shipEntities.size()+"条更新数据");
                info= "{\"ts\":\"" + DateUtil.now() + "\",\"update\":" + gson.toJson(shipEntities) + ",\"add\":[],\"delete\":[]}";
            }
            if("Delete".equals(type)){
                log.info("推送了"+shipEntities.size()+"条删除数据");
                info= "{\"ts\":\"" + DateUtil.now() + "\",\"update\":[],\"add\":[],\"delete\":"+gson.toJson(shipEntities)+"}";
            }
            if("Add".equals(type)){
                log.info("推送了"+shipEntities.size()+"条增加数据");
                info= "{\"ts\":\"" + DateUtil.now() + "\",\"update\":[],\"add\":"+gson.toJson(shipEntities)+",\"delete\":[]}";
            }

            String msg = ZipUtil.gzip(info);
            for (Channel channel : GlobalUtil.channels) {
                channel.writeAndFlush(new TextWebSocketFrame(msg));
            }

        }
    }

    /**
     * 推送全部数据
     */
    public void pushAllShipData(){
        String newKey = CacheConsts.openProvince+"_new";
        if (redisTemplate.hasKey(newKey)) {
            List <ShipEntity> newShipData = (List <ShipEntity>) redisTemplate.opsForValue().get(newKey);
            List <List <ShipEntity>> results = FileUtils.splitList(newShipData, SizeNum);
            for (int i = 0; i < results.size(); i++) {
                List <ShipEntity> shipEntities = results.get(i);
                Gson gson = new GsonBuilder().serializeNulls().create();
                String msg = gson.toJson(shipEntities);
                for (Channel channel : GlobalUtil.channels2) {
                    channel.writeAndFlush(new TextWebSocketFrame(msg));
                }
            }
        }
    }
}
